<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;

/**
 * @title 后台宴会厅设置
 * @description 接口说明
 */
class Banquet extends Api
{
    /**
     * @title 宴会类型
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/index
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * 
     * @return list:列表@
     * @list id:id name:名称 is_hidden:1显示2隐藏 addtime:添加时间 is_meet:1宴席2会议
     * @return page:总页数
     */
    public function index()
    {
        $user = $this->user;
        $page = input('page')??1;
        $name = input('name');
        if($name){
            $where[] = ['name','like',"%".$name."%"];
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];

        $list = db::name('banquet_types')->where($where)
        ->field('name,id,addtime,is_hidden,mid,is_meet')
        ->order('addtime desc')->page("$page,10")->select();
        $total = db::name('banquet_types')->where($where)->count();
        if($list){
            foreach ($list as $k => $v) {
                $list[$k]['addtime'] = date('Y-m-d H:i:s',$list[$k]['addtime']);
            }
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }

    /**
     * @title 宴会类型保存和修改
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_save
     * @method POST
     *  @param name:type type:int require:1 default: other: desc:1新增2修改
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:id type:int require:0 default: other: desc:修改id
     * @param name:is_hidden type:int require:1 default: other: desc:1显示2隐藏
     * @param name:type type:int require:1 default: other: desc:1添加2修改
     * @param name:is_meet type:int require:1 default: other: desc:1宴席2会议
     * 
     */
    public function banquet_save()
    {
        $user = $this->user;
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        if(!input('is_hidden')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $type = input('type');
        $id = input('id');
        $up['name'] = input('name');
        $up['is_meet'] = input('is_meet');
        if($type == 2){
            $up['is_hidden'] = input('is_hidden');
            if(!$id){
                return $this->ajaxReturn(1,'缺少参数');
            }
            db::name('banquet_types')->where('id',$id)->update($up);
        }elseif($type == 1){
            $up['addtime'] = time();
            $up['is_hidden'] = 1;
            $up['hid'] = $user['hid'];
            db::name('banquet_types')->insert($up);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 宴会类型修改获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_edit
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     * @return list:列表@
     * @list id:id name:名称 is_hidden:1显示2隐藏 is_meet:1宴席2会议
     */
    public function banquet_edit()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $elegant = db::name('banquet_types')->where('id',$id)->field('id,name,is_meet,is_hidden')->find();
        $data['list'] = $elegant;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 宴会类型删除
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     */
    public function banquet_del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        // $where[] = ['tid','=',$id];
        // $where[] = ['hid','=',$user['hid']];

        // $time = date('Y-m-d',time());
        // $str = explode("-",$time);
        // $where[] = ['begin_year','=',$str[0]];
        // $where[] = ['begin_month','>=',$str[1]];
        // $where[] = ['begin_day','>=',$str[2]];
        // $where[] = ['type','neq',3];

        // $order = db::name('scheduled')->where($where)->select();
        // if($order){
        //     return $this->ajaxReturn(1,'此宴会类型有预定的订单不可删除');
        // }
        // $wheres[] = ['hid','=',$user['hid']];
        db::name('banquet_types')->where('id',$id)->update(['is_del'=>2]);
        // db::name('banquet_name')->where('tid',$id)->where($where)->delect();
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 宴会类型修改状态
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_change
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     */
    public function banquet_change()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $status = db::name('banquet_types')->where('id',$id)->value('is_hidden');
        if($status == 1){
            $up['is_hidden'] = 2;
            db::name('banquet_types')->where('id',$id)->update($up);
        }elseif($status == 2){
            $up['is_hidden'] = 1;
            db::name('banquet_types')->where('id',$id)->update($up);       
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台餐标管理
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_ds
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:name type:int require:0 default: other: desc:搜索名称
     * 
     * @return list:列表@
     * @list id:id name:名称 money:金额 addtime:添加时间 is_hidden:1显示2隐藏
     * @return page:总页数
     */
    public function banquet_ds()
    {
        $user = $this->user;
        $page = input('page')??1;
        $name = input('name');
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];
        $where[] = ['name','like',"%".$name."%"];
        $list = db::name('ds')->where($where)
        ->field('name,id,addtime,money,is_hidden')
        ->order('addtime desc')->page("$page,10")->select();
        $total = db::name('ds')->where($where)->count();
        if($list){
            foreach ($list as $k => $v) {
                $list[$k]['addtime'] = date('Y-m-d H:i:s',$list[$k]['addtime']);
            }
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;

        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 后台餐标删除
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_delete
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     */
    public function banquet_delete()
    {
        $user = $this->user;
        $id = input('id');
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        db::name('ds')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
     /**
     * @title 餐标修改状态
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/change_ds
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     */
    public function change_ds()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $status = db::name('ds')->where('id',$id)->value('is_hidden');
        if($status == 1){
            $up['is_hidden'] = 2;
            db::name('ds')->where('id',$id)->update($up);
        }elseif($status == 2){
            $up['is_hidden'] = 1;
            db::name('ds')->where('id',$id)->update($up);       
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台菜品首页
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/ds_index
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:tid type:int require:0 default: other: desc:菜品分类
     * @param name:name type:int require:0 default: other: desc:菜品名称
     * 
     * @return list:列表@
     * @list id:id name:菜名 money:金额 unit:1份2盘 alias:别名 is_hidden:1显示2隐藏
     * @return dish_type:列表@
     * @dish_type id:id name:名称 
     * @return page:总页数
     * 
     */
    public function ds_index()
    {
        $user = $this->user;
        // 搜索菜品名称
        $name = input('name');
        // 菜品分类
        $tid = input('tid');
        $page = input('page')??1;
        $where[] = ['is_hidden','=','1'];
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];

        $dish_type = db::name('dish_types')->where($where)->field('id,name')->select();
        if(!$tid){
            $tid = $dish_type[0]['id'];
        }
        $where[] = ['name','like',"%".$name."%"];
        $where[] = ['tid','=',$tid];
        $list = db::name('dish')->where($where)->field('id,name,money,unit,alias,is_hidden')->page("$page,10")->select();
        $total = db::name('dish')->where($where)->count();
        $data['list'] = $list;
        $data['dish_type'] = $dish_type;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;

        return $this->ajaxReturn(0,'成功',$data);    
    }
    /**
     * @title 后台添加菜品分类
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/ds_type
     * @method POST
     * @param name:name type:int require:1 default: other: desc:分类名称
     * 
     */
    public function ds_type()
    {
        $user = $this->user;
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入分类名称');
        }
        $data['name'] = input('name');
        $data['is_hidden'] = 1;
        $data['hid'] = $user['hid'];
        db::name('dish_types')->insert($data);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台删除菜品分类
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/ds_type_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:分类id
     * 
     */
    public function ds_type_del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $dish = db::name('dish')->where('tid',$id)->where('is_del',1)->select();
        if($dish){
            return $this->ajaxReturn(1,'此分类里面有菜品，不能删除');
        }else{
            db::name('dish_types')->where('id',$id)->update(['is_del'=>2]);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台添加修改菜品
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/save_ds
     * @method POST
     * @param name:name type:int require:1 default: other: desc:菜名
     * @param name:money type:int require:1 default: other: desc:金额
     * @param name:unit type:int require:1 default: other: desc:单位
     * @param name:alias type:int require:1 default: other: desc:别名
     * @param name:tid type:int require:0 default: other: desc:分类id
     * @param name:id type:int require:0 default: other: desc:需修改的id
     * 
     * @param name:type type:int require:0 default: other: desc:1添加2修改
     */
    public function save_ds()
    {
        $user = $this->user;
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入菜名');
        }
        if(!input('money')){
            return $this->ajaxReturn(1,'请输入金额');
        }
        if(!input('unit')){
            return $this->ajaxReturn(1,'请输入单位');
        }
        if(!input('alias')){
            return $this->ajaxReturn(1,'请输入别名');
        }
        $type = input('type');
        $data['name'] = input('name');
        $data['money'] = input('money');
        $data['unit'] = input('unit');
        $data['alias'] = input('alias');
        if($type == 1){
            $data['tid'] = input('tid');
            $data['is_hidden'] = 1;
            $data['hid'] = $user['hid'];
            $data['addtime'] = time();
            db::name('dish')->insert($data);
        }elseif($type == 2){
            $id = input('id');
            if(!$id){
                return $this->ajaxReturn(1,'缺少参数');
            }
            db::name('dish')->where('id',$id)->update($data);
        }
       
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台修改菜品获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/edit_ds
     * @method POST
     * @param name:id type:int require:1 default: other: desc:菜品id
     * 
     * @return list:列表@
     * @list id:id name:名称 money:价格 unit:1份2盘 alias:别名
     * 
     */
    public function edit_ds()
    {
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $where = ['id[=]'=>$id];
        $dish = db::name('dish')->where('id',$id)->field('id,name,money,unit,alias')->find();
        $data['list'] = $dish;

        // $datas = ['name','money','unit','alais'];
        // $table = 'dish';
        // $ds = $this->getone($datas,$where,$table);
        // $data['list'] = $ds;

        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 删除菜品
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/ds_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:菜品id
     * 
     * 
     */
    public function ds_del()
    {
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        db::name('dish')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 菜品的启用禁用
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/ds_change
     * @method POST
     * @param name:id type:int require:1 default: other: desc:菜品id
     * 
     */
    public function ds_change()
    {
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $res  = db::name('dish')->where('id',$id)->find();
        if($res['is_hidden']){
            $data['is_hidden'] = '2';
        }elseif($res['is_hidden'] == 2){
            $data['is_hidden'] = '1';
        }
        db::name('dish')->where('id',$id)->update($data);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 添加餐标获取菜品
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/meal_add
     * @method POST
     * @param name:name type:int require:0 default: other: desc:搜索名称
     * 
     * @return type:列表@
     * @type id:id name:名称
     * 
     * @return dish:菜品@
     * @dish id:id name:名称 money:金额
     */
    public function meal_add()
    {
        $user = $this->user;
        $id = input('id');
        $name = input('name');
        $where[] = ['hid','=',$user['hid']];
        $dish_types = db::name('dish_types')->where($where)->field('name,id')->select();
        foreach ($dish_types as $ks => $vs) {
            $wheres[] = ['name','like',"%".$name."%"];
            $dish_types[$ks]['dish'] = db::name('dish')->where('tid',$vs['id'])->where($wheres)->field('name,id,money')->select();
        }
        // print_R($dish_types);die;


        // if($id){
        //     $where[] = ['tid','=',$id];
        // }else{
        //     $res = db::name('dish_types')->where($where)->field('name,id')->find();
        //     $where[] = ['tid','=',$res['id']];
        // }
        
        // $dish = db::name('dish')->where($where)->field('name,id,money')->select();
        // $wheres[] = ['hid','=',$user['hid']];
        // $type = db::name('dish_types')->where($wheres)->field('name,id')->select();
        // $data['type'] = $type;
        $data['dish_types'] = $dish_types;

        return $this->ajaxReturn(0,'成功',$data);
    }
     /**
     * @title 保存餐标
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/meal_save
     * @method POST
     * @param name:name type:int require:1 default: other: desc:餐标名称
     * @param name:money type:int require:0 default: other: desc:餐标金额
     * @param name:did type:int require:0 default: other: desc:菜品id
     * 
     */
    public function meal_save()
    {
        $user = $this->user;
        $did = input('did');
        $money = input('money');
        $name = input('name');
        if(!input('did')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('money')){
            return $this->ajaxReturn(1,'请输入价格');
        }
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        $data['name'] = $name;
        $data['moeny'] = $money;
        $data['did'] = $did;
        $data['addtime'] = time();
        $data['is_hidden'] = 1;
        $data['hid'] = $user['hid'];
        db::name('ds')->insert($data);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台宴会厅首页
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet
     * @method POST
     * @param name:name type:int require:0 default: other: desc:搜索名称
     * @param name:page type:int require:1 default: other: desc:页码
     * 
     * @return list:列表@
     * @list id:id name:名称 max:最大桌数 excellent:最优桌数 tid:类型 did:餐标
     *  @return page:总页数
     */
    public function banquet()
    {
        $user = $this->user;
        $name = input('name');
        $page = input('page')??1;
        if($name){
            $where[] = ['name','like',"%".$name."%"];

        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];

        // $where[] = ['id','=',14];

        $banquet = db::name('banquet')->where($where)->field('id,name,max,excellent,tid,did')->page("$page,10")->select();
        $total = db::name('banquet')->where($where)->count();
        
        foreach ($banquet as $k => $v) {
            if($v['tid']){
                $tid = explode( ",",$v['tid']);
                $count = count($tid);

                if($count != 1){
                    for ($i=0; $i <count($tid) ; $i++) {
                        $res[$i] = db::name('banquet_types')->where('id',$tid[$i])->where('is_del',1)->value('name');
                    }
                }else{
                    $res[] = db::name('banquet_types')->where('id',$v['tid'])->where('is_del',1)->value('name');
                }
                // print_r($res);die;
                $banquet[$k]['tid'] = $res;
            }
            if($v['did']){
                $did = explode( ",",$v['did']);
                $count = count($did);

                if($count != 1){
                    for ($i=0; $i <count($did) ; $i++) { 
                        $datas[$i] = db::name('ds')->where('id',$did[$i])->where('is_del',1)->value('name');
                    }
                }else{
                    $datas[] = db::name('ds')->where('id',$v['did'])->where('is_del',1)->value('name');
                }
                $banquet[$k]['did'] = $datas;
            }
        }
        $data['list'] = $banquet;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 添加后台宴会厅获取类型与餐标
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_add
     * @method POST
     * 
     * @return list:列表@
     * @list banquet_type:类型@ ds:餐标@
     * @banquet_type id:id name:名称
     * @ds id:id name:名称
     * 
     */
    public function banquet_add()
    {
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_hidden','=',1];
        $where[] = ['is_del','=',1];

        $banquet_type = db::name('banquet_types')->where($where)->field('id,name')->select();
        $list['banquet_type'] = $banquet_type;
        $ds = db::name('ds')->where($where)->field('id,name')->select();
        $list['ds'] = $ds;
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 保存后台宴会厅
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_saves
     * @method POST
     * @param name:name type:int require:0 default: other: desc:修改id
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:max type:int require:1 default: other: desc:最大桌数
     * @param name:excellent type:int require:1 default: other: desc:最优桌数
     * @param name:tid type:int require:1 default: other: desc:类型id多类型时id，隔开
     * @param name:did type:int require:1 default: other: desc:餐标id多餐标时id，隔开
     * @param name:money type:int require:1 default: other: desc:最低金额
     * 
     * 
     */

    public function banquet_saves()
    {
        $user = $this->user;
        $id = input('id');
        $did = input('did');
        $tid = input('tid');
        $money = input('money');
        $name = input('name');
        $max = input('max');
        $excellent = input('excellent');
        if(!input('did')){
            return $this->ajaxReturn(1,'请选择餐标');
        }
        if(!input('money')){
            return $this->ajaxReturn(1,'请输入最低金额');
        }
        if(!input('tid')){
            return $this->ajaxReturn(1,'请选择类型');
        }
        if(!input('max')){
            return $this->ajaxReturn(1,'请输入最大桌数');
        }
        if(!input('excellent')){
            return $this->ajaxReturn(1,'请输入最优桌数');
        }
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        $up['name'] = $name;
        $up['max'] = $max;
        $up['excellent'] = $excellent;
        $up['tid'] = $tid;
        $up['did'] = $did;
        $up['money'] = $money;
        if($id){
            db::name('banquet')->where('id',$id)->update($up);
        }else{
            $up['addtime'] = time();
            $up['hid'] = $user['hid'];
            db::name('banquet')->insert($up);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 修改宴会厅获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_up
     * @method POST
     * @param name:id type:int require:1 default: other: desc:宴会厅id
     * 
     */
    public function banquet_up()
    {
        $user = $this->user;
        $id = input('id');
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }

        $banquet = db::name('banquet')->where('id',$id)->field('id,name,max,excellent,tid,money,did')->find();
        if($banquet){
            $tid = explode(',',$banquet['tid']);
            $did = explode(',',$banquet['did']);
            $tidc = count($tid);
            $didc = count($did);

        
            for ($i=0; $i <$tidc ; $i++) { 
                $types = db::name('banquet_types')->where('id',$tid[$i])->where('is_del',1)->field('id,name')->find();
                if($types){
                    $type[] = $types;
                }else{
                    $type = [];

                }
            }
            for ($i=0; $i <$didc ; $i++) { 
                $ds = db::name('ds')->where('id',$did[$i])->where('is_del',1)->field('id,name')->find();
                if($ds){
                    $dsa[] = $ds;
                }else{
                    $dsa = [];
                }
            }
            
                $banquet['tid'] = $type;
                $banquet['did'] = $dsa;
        }
        return $this->ajaxReturn(0,'成功',$banquet);
    }
    /**
     * @title 删除后台宴会厅
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_dels
     * @method POST
     * @param name:id type:int require:1 default: other: desc:宴会厅id
     * 
     */
    public function banquet_dels()
    {
        // $user = $this->user;
        $id = input('id');
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        // $time = date('Y-m-d',time());
        // $str = explode("-",$time);
        // $where[] = ['begin_year','=',$str[0]];
        // $where[] = ['begin_month','>=',$str[1]];
        // $where[] = ['begin_day','>=',$str[2]];
        // $where[] = ['bid','=',$id];
        // $where[] = ['type','neq',3];
        // $scheduled = db::name('scheduled')->where($where)->select();
        // if($scheduled){
        //     return $this->ajaxReturn(1,'此宴会厅有未完成的订单');
        // }else{
        //     db::name('banquet')->where('id',$id)->delete();
        // }
        db::name('banquet')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
     /**
     * @title 后台押金配置
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/deposit
     * @method POST
     * @param name:type type:int require:1 default: other: desc:1押金2支付方式3担保人
     * 
     * @return list:列表@
     * @list id:id name:名称 mobile:手机号（押金与支付方式无此参数） is_hidden:1显示2隐藏
     * @return config:1开启多次填写押金2关闭多次填写押金
     */
    public function deposit()
    {
        $user = $this->user;
        $type = input('type');
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $where[] = ['hid','=',$user['hid']];
        if($type == 1){
            $where[] = ['type','=',1];
            $where[] = ['is_del','=',1];

            $deposit = db::name('deposit')->where($where)->field('id,name,is_hidden')->select();
        }elseif($type == 2){
            $where[] = ['type','=',2];
            $where[] = ['is_del','=',1];

            $deposit = db::name('deposit')->where($where)->field('id,name,is_hidden')->select();
        }elseif($type == 3){
            $where[] = ['is_del','=',1];

            $deposit = db::name('guarantee')->where($where)->field('id,name,mobile,is_hidden')->select();
        }
        $config = db::name('config')->where('hid',$user['hid'])->field('id,status')->find();
        if(!$config){
            $up['name'] = '多次填写押金';
            $up['hid'] = $user['hid'];
            $up['type'] = 1;
            $up['status'] = 1;
            $cid = db::name('config')->insertGetid($up);
            $config['status'] = $up['status'];
            $config['id'] = $cid;

        }
        $data['list'] = $deposit;
        $data['config'] = $config;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 后台押多次填写押金
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/change
     * @method POST
     * @param name:id type:int require:0 default: other: desc:需修改id
     * @param name:status type:int require:0 default: other: desc:状态1开启2关闭
     */
    public function change()
    {
        $user = $this->user;
        $status = input('status');
        $id = input('id');
        if(!$id || !$status){
            $this->ajaxReturn(1,'缺少参数');
        }
        if($status == 1){
            $up['status'] = 2;
        }else{
            $up['status'] = 1;
        }
        if(db::name('config')->where('id',$id)->where('hid',$user['hid'])->update($up)){
            return $this->ajaxReturn(0,'成功');
        }else{
            return $this->ajaxReturn(1,'失败');
        }
    }
     /**
     * @title 后台押金添加与编辑
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/deposit_save
     * @method POST
     * @param name:type type:int require:1 default: other: desc:1押金2支付方式3担保人
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:mobile type:int require:0 default: other: desc:手机号
     * @param name:is_type type:int require:1 default: other: desc:1新增2修改
     * @param name:id type:int require:0 default: other: desc:需修改id
     */
    public function deposit_save()
    {
        $user = $this->user;
        $type = input('type');
        $id = input('id');
        $is_type = input('is_type');
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('name')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $data['hid'] = $user['hid'];
        $data['name'] = input('name');
        if($is_type == 1){
            if($type == 1){
                $data['type'] = 1;
                db::name('deposit')->insert($data);
            }elseif($type == 2){
                $data['type'] = 2;
                db::name('deposit')->insert($data);
            }elseif($type == 3){
                if(!input('mobile')){
                    return $this->ajaxReturn(1,'缺少参数');
                }
                $data['mobile'] = input('mobile');
                db::name('guarantee')->insert($data);
            }
        }elseif($is_type == 2){
            if($type == 1 || $type == 2){
                db::name('deposit')->where('id',$id)->update($data);
            }elseif($type == 3){
                $data['mobile'] = input('mobile');
                db::name('guarantee')->where('id',$id)->update($data);
            }
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台押金配置删除
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/deposit_del
     * @method POST
     * @param name:type type:int require:1 default: other: desc:1押金2支付方式3担保人
     * @param name:id type:int require:1 default: other: desc:id
     */
    public function deposit_del()
    {
        $user = $this->user;
        $id = input('id');
        $type = input('type');
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        // $time = date('Y-m-d',time());
        // $str = explode("-",$time);
        // $where[] = ['begin_year','=',$str[0]];
        // $where[] = ['begin_month','>=',$str[1]];
        // $where[] = ['begin_day','>=',$str[2]];
        // $where[] = ['type','neq',3];
        // $where[] = ['hid','=',$user['hid']];
        if($type == 1 || $type == 2){
            // $res =  db::name('scheduled')->where($where)->select();
            // if($res){
            //     foreach ($res as $k => $v) {
            //         $wheres[] = ['type_id','=',$id];
            //         $wheres[] = ['sid','=',$v['id']];
            //         $scheduled_deposit =  db::name('scheduled_deposit')->where($where)->select();
            //         if($scheduled_deposit){
            //             return $this->ajaxReturn(1,'有未完成订单不可删除');
            //         }
                    db::name('deposit')->where('id',$id)->update(['is_del'=>2]);
            //     }
            // }
        }elseif($type == 3){
            // $where[] = ['vouch_id','=',$id];
            // $res =  db::name('scheduled')->where($where)->select();
            // if($res){
            //     return $this->ajaxReturn(1,'有未完成订单不可删除');
            // }
            db::name('guarantee')->where('id',$id)->update(['is_del'=>2]);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 后台押金隐藏与显示
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/deposit_change
     * @method POST
     * @param name:type type:int require:1 default: other: desc:1押金2支付方式3担保人
     * @param name:id type:int require:1 default: other: desc:id
     */
    public function deposit_change()
    {
        $user = $this->user;
        $id = input('id');
        $type = input('type');
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if($type == 1 || $type == 2){
            $res = db::name('deposit')->where('id',$id)->value('is_hidden');
            if($res == 1){
                $up['is_hidden'] = 2;
                db::name('deposit')->where('id',$id)->update($up);
            }elseif($res == 2){
                $up['is_hidden'] = 1;
                db::name('deposit')->where('id',$id)->update($up);
            }
        }elseif($type == 3){
            $res = db::name('guarantee')->where('id',$id)->value('is_hidden');
            if($res == 1){
                $up['is_hidden'] = 2;
                db::name('guarantee')->where('id',$id)->update($up);
            }elseif($res == 2){
                $up['is_hidden'] = 1;
                db::name('guarantee')->where('id',$id)->update($up);
            }
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 宴会类型统计导出（未补充）
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/banquet/banquet_export
     * @method POST
     */
    public function banquet_export()
    {
        $user = $this->user;
        // 查询数据
        $where[] = ['hid','=',$user['hid']];

        $type = db::name('banquet_types')->where('hid',$user['hid'])->field('id,name')->select();
        $where[] = ['type','neq',1];	
        $where[] = ['is_del','=',2];
        // 场次
        $scheduled = db::name('scheduled')->where($where)->field('id,tid')->select();
        // 结单订单
        $check = db::name('check_order')->where('hid',$user['hid'])->select();
        // foreach ($scheduled as $k => $v) {
        //     foreach ($check as $kc => $vc) {
        //         if($v['id'] == $vc['sid']){
        //             $scheduled[$k]['desk'] = $vc['desk'];
        //         }
        //     }
        // }
        $newArr = $this->dataGroup($scheduled,'tid');
        foreach ($type as $ks => $vs) {
            foreach ($newArr as $kn => $vn) {
                if($vs['id'] == $kn){
                    $type[$ks]['field'] = count($vn);
                    // foreach ($vn as $kv => $vv) {
                    //     if($vv['desk']){
                    //         $type[$ks]['desk'] .= $vv['desk'];
                    //     }
                        
                    // }
                    // print_r($vn);die;
                    
                }
            }
        }
        // $count = db::name('scheduled')->where($where)->count();
        print_r($type);die;
        // 设置表头信息（对应字段名,宽度，显示表头名称）
        $cellName = [
            ['id', 'auto', 'ID'],
            ['name', 'auto', '渠道名称'],
            ['num', 'auto', '获客数'],
            ['deal', 'auto', '成交数'],
            ['effective', 'auto', '有效率'],
        ];
        // 调用插件（传入插件名，[导出文件名、表头信息、具体数据]）
        plugin_action('Excel/Excel/export', ['渠道来源统计', $cellName, $newArr]);
    }
    protected function dataGroup(array $dataArr,string $keyStr)
    {
        $newArr=[];
        foreach ($dataArr as $k => $val) {    //数据根据日期分组
            $newArr[$val[$keyStr]][] = $val;
        }
        return $newArr;
    }

}